package com.vdong.trade.trading.web.controller;

import com.github.pagehelper.PageInfo;
import com.vdong.trade.trading.common.constants.TradingConstants;
import com.vdong.trade.trading.common.contract.ApiContract;
import com.vdong.trade.trading.common.exception.ProcessException;
import com.vdong.trade.trading.common.exception.ServiceException;
import com.vdong.trade.trading.common.foundation.FoundationController;
import com.vdong.trade.trading.entity.dto.param.WithdrawDTO;
import com.vdong.trade.trading.entity.po.SettlementTotalParamDTO;
import com.vdong.trade.trading.entity.po.WithdrawAuditPO;
import com.vdong.trade.trading.entity.po.WithdrawPO;
import com.vdong.trade.trading.entity.po.WithdrawParamDTO;
import com.vdong.trade.trading.entity.vo.WithdrawDetailVO;
import com.vdong.trade.trading.entity.vo.WithdrawRecordListVO;
import com.vdong.trade.trading.facade.enums.UserTypeEnum;
import com.vdong.trade.trading.service.WithdrawService;
import lombok.extern.slf4j.Slf4j;
import org.lamb.framework.common.remote.FacadeResult;
import org.lamb.framework.common.userDTO.AccountSystemUserInfoDTO;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.StringUtil;
import org.lamb.framework.core.security.LambPrincipalFactoryContainer;
import org.lamb.framework.core.templete.LambResponseTemplete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.math.BigDecimal;

import static com.vdong.trade.trading.common.enums.ProcessExceptionEnum.*;

/**
 * @author zhoulongbiao
 * @Date 2018/12/10 14:27
 */
@RestController
@CrossOrigin
@Slf4j
public class WithdrawController extends FoundationController {

    @Autowired
    private WithdrawService withdrawService;

    /**
     * 提现记录列表
     */
    @RequestMapping(value = ApiContract.I000000010, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> withdrawRecordList(@RequestBody WithdrawParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        if ("-1".equals(param.getWithdrawStatus())) {
            param.setWithdrawStatus(null);
        }

        // 获取当前商户/saas用户缓存信息
        AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
        if (accountSystemUser == null) {
            log.error("获取商户用户缓存信息失败!");
            return Mono.error(new ProcessException(EB00000005));
        }
        param.setUserId(Long.valueOf(accountSystemUser.getSysUserId()));

        return returning(CopyUtil.copyObject(withdrawService.withdrawRecordList(param), new PageInfo<WithdrawRecordListVO>()));
    }

    /**
     * 提现详情
     */
    @RequestMapping(value = ApiContract.I000000011, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> withdrawDetail(@RequestBody WithdrawParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(withdrawService.withdrawDetail(param), new WithdrawDetailVO()));
    }

    /**
     * 提现申请列表
     */
    @RequestMapping(value = ApiContract.I000000012, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> withdrawApplyList(@RequestBody WithdrawParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));

        // 获取当前商户用户缓存信息
        AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
        if (accountSystemUser == null) {
            log.error("商户提现获取用户缓存信息失败!");
            return Mono.error(new ProcessException(EB00000005));
        }

        if (StringUtil.hasBlank(accountSystemUser.getSysUserId())) {
            log.error("推客提现获取用户缓存userId为空!");
            return Mono.error(new ProcessException(EB00000005));
        }

        param.setUserId(Long.valueOf(accountSystemUser.getSysUserId()));

        return returning(withdrawService.withdrawApplyList(param));
    }


    /**
     * 提现申请详情
     */
    @RequestMapping(value = ApiContract.I000000013, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> withdrawApplyDetail(@RequestBody WithdrawParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(withdrawService.withdrawApplyDetail(param));
    }

    /**
     * 转账申请列表
     */
    @RequestMapping(value = ApiContract.I000000003, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> transferApplyList(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(withdrawService.transferApplyList(param));
    }

    /**
     * 转账确认或者拒绝
     */
    @RequestMapping(value = ApiContract.I000000004, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> transferConfirmOrRefuse(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(withdrawService.transferConfirmOrRefuse(param));
    }

    /**
     * 转账记录列表
     */
    @RequestMapping(value = ApiContract.I000000005, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> transferRecordList(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(withdrawService.transferRecordList(param));
    }

    /**
     * 转账记录详情
     */
    @RequestMapping(value = ApiContract.I000000006, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> transferRecordDetail(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(withdrawService.transferRecordDetail(param));
    }

    /**
     * 转账申请金额和申请笔数
     */
    @RequestMapping(value = ApiContract.I000000024, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> moneyAndCount(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param.getUserId(), param.getUserType()))
            return Mono.error(new ProcessException(EB00000008));
        return returning(withdrawService.moneyAndCount(param));
    }

    /**
     * 推客提现
     *
     * @param param
     * @return
     */
    @PostMapping(value = ApiContract.I000000019, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> userWithdraw(@RequestBody WithdrawPO param) {
        try {
            if (StringUtil.hasBlank(param)) {
                return returning(FacadeResult.buildFail(EB00000008.getMessage()));
            }
            if (StringUtil.hasBlank(param.getAmount(), param.getOpenid())) {
                return returning(FacadeResult.buildFail(EB00000008.getMessage()));
            }
            if (new BigDecimal(param.getAmount()).compareTo(BigDecimal.ZERO) <= 0) {
                return returning(FacadeResult.buildFail(EB00000009.getMessage()));
            }

            WithdrawDTO withdrawDTO = new WithdrawDTO();
            CopyUtil.copyObject(param, withdrawDTO);

            withdrawDTO.setIp(TradingConstants.IP);

            FacadeResult<String> result = withdrawService.userWithdraw(withdrawDTO);
            return returning(result);
        } catch (ServiceException e) {
            return returning(FacadeResult.buildFail(e.getMessage()));
        }
    }

    /**
     * 商户提现
     *
     * @param withdrawPO
     * @return
     */
    @PostMapping(value = ApiContract.I000000022, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> merchantWithdraw(@RequestBody WithdrawPO withdrawPO) {
        try {
            if (StringUtil.hasBlank(withdrawPO)) {
                return returning(FacadeResult.buildFail(EB00000008.getMessage()));
            }
            if (StringUtil.hasBlank(withdrawPO.getAmount(), withdrawPO.getBankId())) {
                return returning(FacadeResult.buildFail(EB00000008.getMessage()));
            }
            if (new BigDecimal(withdrawPO.getAmount()).compareTo(BigDecimal.ZERO) <= 0) {
                return returning(FacadeResult.buildFail(EB00000009.getMessage()));
            }

            WithdrawDTO withdrawDTO = new WithdrawDTO();
            CopyUtil.copyObject(withdrawPO, withdrawDTO);
            FacadeResult<String> result = withdrawService.merchantWithdraw(withdrawDTO);
            return returning(result);
        } catch (ServiceException e) {
            return returning(FacadeResult.buildFail(e.getMessage()));
        }
    }

    /**
     * 商户提现审核
     *
     * @param withdrawAuditPO
     * @return
     */
    @PostMapping(value = ApiContract.I000000023, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> withdrawAudit(@RequestBody WithdrawAuditPO withdrawAuditPO) {
        try {
            if (withdrawAuditPO == null) {
                return returning(FacadeResult.buildFail(EB00000008.getMessage()));
            }

            if (StringUtil.hasBlank(withdrawAuditPO.getId(), withdrawAuditPO.getWithdrawStatus())) {
                return returning(FacadeResult.buildFail(EB00000008.getMessage()));
            }

            WithdrawDTO withdrawDTO = new WithdrawDTO();
            CopyUtil.copyObject(withdrawAuditPO, withdrawDTO);

            FacadeResult<String> result = withdrawService.withdrawAudit(withdrawDTO);
            return returning(result);
        } catch (ServiceException e) {
            return returning(FacadeResult.buildFail(e.getMessage()));
        }
    }

    /**
     * 商户提现配置
     *
     * @param withdrawPO
     * @return
     */
    @PostMapping(value = ApiContract.I000000025, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> withdrawConfig(@RequestBody WithdrawPO withdrawPO) {

        // 获取当前商户/saas用户缓存信息
        AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
        if (accountSystemUser == null) {
            log.error("获取商户用户缓存信息失败!");
            return Mono.error(new ProcessException(EB00000005));
        }

        WithdrawDTO withdrawDTO = new WithdrawDTO();
        withdrawDTO.setUserId(Long.valueOf(accountSystemUser.getSysUserId()));
        withdrawDTO.setUserType(UserTypeEnum.MERCHANT.getValue().toString());
        return returning(withdrawService.withdrawConfig(withdrawDTO));

    }

}
